package com.hxkcloud.patient.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.patient.beans.LoginInfo;
import com.hxkcloud.patient.beans.LogininfoVerifyBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-07-21.
 * Description:操作登录用户表
 */
public class LoginInfoDao {
    private Context context;
    private Dao<LoginInfo, Integer> loginInfoDao;
    private VerifyDao verifyDao;

    public LoginInfoDao(Context context) {
        this.context = context;
        try {
            loginInfoDao = DatabaseHelper.getHelper(context).getDao(LoginInfo.class);
            verifyDao = new VerifyDao(context);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public LoginInfo getUser() {
        try {
            if (loginInfoDao.queryForAll() != null && loginInfoDao.queryForAll().size() > 0) {
                LoginInfo loginInfo = loginInfoDao.queryForAll().get(0);
                loginInfo.setInformationImageList(verifyDao.getImgs(loginInfo.getUserId()));
                return loginInfo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setUser(LoginInfo user) {
        try {
            delUser();
            loginInfoDao.create(user);
            if (user.getInformationImageList() != null && user.getInformationImageList().size() > 0) {
                List<LogininfoVerifyBean> values = new ArrayList<>();
                for (String img : user.getInformationImageList()) {
                    LogininfoVerifyBean b = new LogininfoVerifyBean();
                    b.setImg(img);
                    b.setUserId(user.getUserId());
                    values.add(b);
                }
                verifyDao.setValue(values);
            }
        } catch (SQLException e) {
        }
    }

    public void delUser() {
        try {
//            loginInfoDao.delete(getUser());
            TableUtils.clearTable(loginInfoDao.getConnectionSource(), LoginInfo.class);
            verifyDao.clearValue();
        } catch (SQLException e) {
        }
    }

    public class VerifyDao {
        private Context context;
        private Dao<LogininfoVerifyBean, Integer> verifyDao;

        public VerifyDao(Context context) {
            this.context = context;
            try {
                verifyDao = DatabaseHelper.getHelper(context).getDao(LogininfoVerifyBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public List<String> getImgs(String uId) {
            try {
                QueryBuilder builder = verifyDao.queryBuilder();
                builder.where().eq("userId", uId);
                List<LogininfoVerifyBean> list = builder.query();
                List<String> lists = new ArrayList<>();
                for (LogininfoVerifyBean b : list) {
                    lists.add(b.getImg());
                }
                return lists;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }

        public void setValue(List<LogininfoVerifyBean> values) {
            try {
                for (LogininfoVerifyBean v : values) {
                    verifyDao.create(v);
                }
            } catch (SQLException e) {
            }
        }

        public void setValue(LogininfoVerifyBean value) {
            try {
                verifyDao.create(value);
            } catch (SQLException e) {
            }
        }

        public void clearValue() {
            try {
                TableUtils.clearTable(verifyDao.getConnectionSource(), LogininfoVerifyBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
